<template>
	<view class="content">
		<input type="hidden" name="id" v-model="addressData.id"  style="display:none">
		<input type="hidden" name="uid" v-model="addressData.uid"  style="display:none">
		<view class="row b-b">
			<text class="tit">联系人</text>
			<input class="input" type="text" name="aname" v-model="addressData.aname" placeholder="收货人姓名" placeholder-class="placeholder" />
		</view>
		<view class="row b-b">
			<text class="tit">手机号</text>
			<input class="input" type="number"  name="aphone" v-model="addressData.aphone" placeholder="收货人手机号码" placeholder-class="placeholder" />
		</view>
		<view class="row b-b">
			<text class="tit">省份</text>
			<input class="input" type="text"  name="address" v-model="addressData.address" placeholder="填写省份" placeholder-class="placeholder" />
		</view>
		<view class="row b-b">
			<text class="tit">城市</text>
			<input class="input" type="text" name="addressname" v-model="addressData.addressname" placeholder="填写城市" placeholder-class="placeholder" />
		</view>
		<view class="row b-b"> 
			<text class="tit">门牌号</text>
			<input class="input" type="text" name="hnumber" v-model="addressData.hnumber" placeholder="小区、楼号、门牌" placeholder-class="placeholder" />
		</view>
		
		<view class="row default-row">
			<text class="tit">设为默认</text>
			<switch :checked="addressData.defaule" color="#fa436a" @change="switchChange" />
		</view>
		<button class="add-btn" @click="confirm" v-if="this.type=='edit'">提交</button>
		<button class="add-btn" @click="addconfirm" v-if="this.type=='add'">提交</button>
		<button class="add-btn" @click="del" v-if="this.type=='add'"style="display:none" >删除</button>
		<button class="add-btn" @click="del" v-if="this.type=='edit'" >删除</button>
	</view>
</template>

<script>
	import {  
	    mapState,
		mapMutations	
	} from 'vuex';  
	export default {
		data() {
			return {
				type:"",
				aname:"",
				aphone:"",
				addressname:"",
				hnumber:"",
				address:"",
				uid:"",
				addressData: {}
			}
		},
		onLoad(option){
			let title = '新增收货地址';
			this.type=option.type
			if(option.type==='edit'){
				title = '编辑收货地址'
				uni.request({
					url:"http://localhost:8091/user/getAddressByid/"+option.data,
					success: (res) => {
						const data=res.data.data
						 this.addressData = data
					}
				})		
				
			}
			this.manageType = option.type;
			uni.setNavigationBarTitle({				
			})
		},
		computed: {
			...mapState(['hasLogin','userInfo'])
		},
		methods: {
			backPageRefresh(){
							let pages = getCurrentPages(); // 当前页面
							let beforePage = pages[pages.length - 2]; // 前一个页面
							console.log("beforePage",beforePage);
							console.log(pages);
							uni.navigateBack({
								delta: 1,
							    success: function() {
							        beforePage.$vm.refreshRequest(); // 执行前一个页面的刷新
							    }
							});
						},
			
			switchChange(e){
				this.addressData.default = e.detail;
			},
			
			//地图选择地址
			chooseLocation(){
				uni.chooseLocation({
					success: (data)=> {
						this.addressData.addressname = data.name;
						this.addressData.address = data.name;
					}
				})
			},
			
			//提交
			confirm(){
				
				uni.request({
					url:"http://localhost:8091/user/updateAdress",
					method:"POST",
					data:this.addressData,
					success: (res) =>{
					 let pages = getCurrentPages(); // 当前页面
					 let beforePage = pages[pages.length - 2]; // 前一个页面
					 console.log("beforePage",beforePage);
					 console.log(pages);
					 this.$api.msg(`地址${this.manageType=='edit' ? '修改': '添加'}成功`);
					 setTimeout(()=>{
					 	uni.navigateBack({
					 	delta: 1,
					     success: function() {
					         beforePage.$vm.loadData(); // 执行前一个页面的刷新
					     }
					 });
					 }, 800)
					 }	
				})
			},
			//添加提交
			addconfirm(){
				let data = {
					aname:this.addressData.aname,
					aphone:this.addressData.aphone,
					addressname:this.addressData.addressname,
					hnumber:this.addressData.hnumber,
					address:this.addressData.address,
					uid:this.userInfo.id,
				};
				uni.request({
					url:"http://localhost:8091/user/insert",
					method:"POST",
					data:data,
					success: (res) =>{
					    let pages = getCurrentPages(); // 当前页面
					    let beforePage = pages[pages.length - 2]; // 前一个页面
					    console.log("beforePage",beforePage);
					    console.log(pages);
						this.$api.msg(`地址${this.manageType=='edit' ? '修改': '添加'}成功`);
						setTimeout(()=>{
							uni.navigateBack({
					    	delta: 1,
					        success: function() {
					            beforePage.$vm.loadData(); // 执行前一个页面的刷新
					        }
					    });
						}, 800)
					    
					 },
					 
				})	
				
			},
			//删除
			del(){
					uni.request({
					url:"http://localhost:8091/user/del/"+this.addressData.id,
					method:"GET",
					success: (res) =>{
					let pages = getCurrentPages(); // 当前页面
					let beforePage = pages[pages.length - 2]; // 前一个页面
					console.log("beforePage",beforePage);
					console.log(pages);
					this.$api.msg(`删除成功`);
					setTimeout(()=>{
						uni.navigateBack({
						delta: 1,
					    success: function() {
					        beforePage.$vm.loadData(); // 执行前一个页面的刷新
					    }
					});
					}, 800)
					 }	
				})
			}
		}
	}
</script>

<style lang="scss">
	page{
		background: $page-color-base;
		padding-top: 16upx;
	}

	.row{
		display: flex;
		align-items: center;
		position: relative;
		padding:0 30upx;
		height: 110upx;
		background: #fff;
		
		.tit{
			flex-shrink: 0;
			width: 120upx;
			font-size: 30upx;
			color: $font-color-dark;
		}
		.input{
			flex: 1;
			font-size: 30upx;
			color: $font-color-dark;
		}
		.icon-shouhuodizhi{
			font-size: 36upx;
			color: $font-color-light;
		}
	}
	.default-row{
		margin-top: 16upx;
		.tit{
			flex: 1;
		}
		switch{
			transform: translateX(16upx) scale(.9);
		}
	}
	.add-btn{
		display: flex;
		align-items: center;
		justify-content: center;
		width: 690upx;
		height: 80upx;
		margin: 60upx auto;
		font-size: $font-lg;
		color: #fff;
		background-color: $base-color;
		border-radius: 10upx;
		box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);
	}
</style>
